package com.activitycode.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.activitycode.datamodel.ground.otherservice.ServiceMsg;
import com.activitycode.tools.PublicTools;
import com.activitycode.util.CommConstant;
import com.duostec.zhuyue.pets.R;
import com.nostra13.universalimageloader.core.ImageLoader;


public class OtherServiceAdapter extends BaseAdapter {
	private Context context;
	private OtherServiceAdapter adapter;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private List<ServiceMsg> list;
	public OtherServiceAdapter(Context context, List<ServiceMsg> list){
		this.context=context;
		this.list=list;
		adapter=this;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.adapter_takephoto_item, null);
			viewHolder.business_name = (TextView) convertView
					.findViewById(R.id.adapter_takephoto_petstorename);
			viewHolder.distance = (TextView) convertView
					.findViewById(R.id.adapter_takephoto_range);
			viewHolder.business_brief = (TextView) convertView
					.findViewById(R.id.adapter_takephoto_detial);
			;
			viewHolder.userHead = (ImageView) convertView
					.findViewById(R.id.adapter_takephoto_imageView1);
			viewHolder.container=(LinearLayout)convertView.findViewById(R.id.adapter_takephoto_linear);
			viewHolder.rating = (RatingBar) convertView
					.findViewById(R.id.adapter_takephoto_realratingBar1);
			convertView.setTag(viewHolder);
		} else {

			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(position%2==0){
			viewHolder.container.setBackground(context.getResources().getDrawable(R.drawable.style_listview_item2));
		}else{
			viewHolder.container.setBackground(context.getResources().getDrawable(R.drawable.style_listview_item3));
		}
		viewHolder.business_name.setText(""+list.get(position).getBusiness_name());
		if (Double.parseDouble(""+list.get(position).getDistance())<1000) {
			viewHolder.distance.setText(PublicTools.storeOnePosition(Double.parseDouble(""+list.get(position).getDistance()))+"M");
		}else {
			viewHolder.distance.setText(PublicTools.storeOnePosition(Double.parseDouble(""+list.get(position).getDistance())/1000)+"KM");
		}
		
		viewHolder.business_brief.setText(""+list.get(position).getBusiness_brief());
		imageLoader.displayImage(CommConstant.SERVICE_HOST_IP_LAN_IMG+list.get(position).getBusiness_image(), 	viewHolder.userHead);
		viewHolder.rating.setRating((float) Double.parseDouble(""+list.get(position).getBusiness_score()));
		return convertView;
	}
	public final static class ViewHolder {
		TextView business_name, business_brief, distance;
		ImageView userHead;
		RatingBar rating;
		LinearLayout container;
	}
}
